﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;
using FX.CC.Andon.Entity.Service;
using FX.CC.Andon.Entity;

namespace FX.CC.Andon.Service.Service
{
    public class KeyValueService : DbHelperBase
    {
        public bool AddListeningLine(IList<KeyValuePairEx<Guid, Guid>> list)
        {
            var result = false;
            string sql = " INSERT INTO UT_ListeningLine Values(@sid, @lineID) ";
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    Db.AddInParameter(cmd, "@sid", DbType.Guid);
                    Db.AddInParameter(cmd, "@lineID", DbType.Guid);

                    int count = 0;
                    foreach (var i in list)
                    {
                        cmd.Parameters["@sid"].Value = i.Key;
                        cmd.Parameters["@lineID"].Value = i.Value;
                        count += Db.ExecuteNonQuery(cmd);
                    }

                    result = count == list.Count;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public void DeleteListeningLine(Guid sessionID)
        {
            string sql = "Delete From UT_ListeningLine Where SessionID=@sid";
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@sid", DbType.Guid, sessionID);
                try
                {
                    Db.ExecuteNonQuery(cmd);
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
        }

        public bool AddListeningError(IList<KeyValuePairEx<Guid, string>> list)
        {
            var result = false;
            string sql = " INSERT INTO UT_ListeningError Values(@sid, @typeCode) ";
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    Db.AddInParameter(cmd, "@sid", DbType.Guid);
                    Db.AddInParameter(cmd, "@typeCode", DbType.String);

                    int count = 0;
                    foreach (var i in list)
                    {
                        cmd.Parameters["@sid"].Value = i.Key;
                        cmd.Parameters["@typeCode"].Value = i.Value;
                        count += Db.ExecuteNonQuery(cmd);
                    }

                    result = count == list.Count;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public void DeleteListeningError(Guid sessionID)
        {
            string sql = "Delete From UT_ListeningError Where SessionID=@sid";
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@sid", DbType.Guid, sessionID);
                try
                {
                    Db.ExecuteNonQuery(cmd);
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
        }

        public IList<KeyValuePairEx<Guid, string>> GetAllWorkShop()
        {
            string sql = @"SELECT WSID, WSName FROM UT_WorkShop WHERE (RecStatus = 1) ORDER BY InputTime;";

            var list = new List<KeyValuePairEx<Guid, string>>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new KeyValuePairEx<Guid, string>();
                            obj.Key = ReaderHelper.GetValue<Guid>(reader, "WSID");
                            obj.Value = ReaderHelper.GetValue<string>(reader, "WSName");
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }

        }

        public IList<KeyValuePairEx<Guid, string>> GetAllLine()
        {
            string sql = @"SELECT LineID, LineName FROM UT_Line ORDER BY InputTime;";

            var list = new List<KeyValuePairEx<Guid, string>>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new KeyValuePairEx<Guid, string>();
                            obj.Key = ReaderHelper.GetValue<Guid>(reader, "LineID");
                            obj.Value = ReaderHelper.GetValue<string>(reader, "LineName");
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }

        }

        public IList<KeyValuePairEx<string, string>> GetAllEType()
        {
            string sql = @"SELECT ETypeCode, ETypeName FROM UT_EType WHERE (RecStatus = 1) ORDER BY InputTime;";
            var list = new List<KeyValuePairEx<string, string>>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new KeyValuePairEx<string, string>();
                            obj.Key = ReaderHelper.GetValue<string>(reader, "ETypeCode");
                            obj.Value = ReaderHelper.GetValue<string>(reader, "ETypeName");
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }

        }
    }
}
